package com.eastfair.venueservice.service.impl;

import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.projectcore.api.DictionaryFeign;
import com.eastfair.projectcore.entity.Dictionary;
import com.eastfair.venueservice.service.VenueManageDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典 service.
 *
 * @author dqq
 * @date 2023 /3/6
 */
@Service
@Slf4j
public class VenueManageDictionaryServiceImpl implements VenueManageDictionaryService {

    @Resource
    private DictionaryFeign dictionaryFeign;

    @Override
    public Map<String, String> queryDictMap(String dictCode) {
        Map<String, String> roleDict = new HashMap<>(16);
        List<Dictionary> dictionaryList = queryDict(dictCode);
        if (dictionaryList != null && !dictionaryList.isEmpty()) {
            for (Dictionary dictionary : dictionaryList) {
                roleDict.put(dictionary.getCode(), dictionary.getName());
            }
        }
        return roleDict;
    }

    @Override
    public List<Dictionary> queryDict(String var) {
        log.info("queryDict - 查询数据字典, dictCode={}", var);
        R<List<Dictionary>> result = dictionaryFeign.queryDict(var);
        if (!result.getIsSuccess()) {
            log.error("queryDict - 查询数据字典失败");
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }
}